import { defineConfig, loadEnv } from "vite";
import react from '@vitejs/plugin-react'


import { babel, getBabelOutputPlugin } from '@rollup/plugin-babel';
import clear from "rollup-plugin-clear"
import { visualizer } from "rollup-plugin-visualizer";
import image from "@rollup/plugin-image";


// https://vitejs.dev/config/
// @ts-ignore
export default defineConfig(({ mode }) => {

  return {
    // define: {
    //   'process.env': loadEnv(mode, process.cwd())
    // },
    plugins: [
      babel({ babelHelpers: 'bundled', exclude: 'node_modules/**', extensions: ['.js', '.ts', '.tsx', '.jsx'] }),
      getBabelOutputPlugin({
        presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript']
      }),


      clear({
        // 必填项, 指出哪些目录应该被清理
        targets: ['dist'],
        // 可选，当在watch模式下进行重新编译时是否清除目录
        watch: false // default: false
      }),
      visualizer(), // 自动开启分析页面
      react(),
      // ts({tsconfigPath: path.resolve(__dirname, "tsconfig.json")}),
      image(),

    ],
    css: {
      preprocessorOptions: {
        scss: { api: 'modern-compiler' },
      }
    },

    resolve: {
      // api: 'modern-compiler',
    //   alias: {
    //     '~': path.resolve(__dirname, 'src')
    //   }
    },
    // envDir: './env',
    // envPrefix: ['VITE_', 'YP_'],
    build: {
      manifest: true,
      outDir: `./dist/${mode}`,
      rollupOptions: {
        external: [/@babel\/runtime/]
      }
    }
  }


})
